from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from .config import settings

# MySQL配置
engine_kwargs = {}
if "mysql" in settings.DATABASE_URL:
    engine_kwargs = {
        "pool_pre_ping": True,
        "pool_recycle": 3600,
        "echo": settings.DEBUG
    }

engine = create_engine(settings.DATABASE_URL, **engine_kwargs)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()